package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Attendance;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.AttendanceQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IAttendanceService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import com.fasterxml.jackson.annotation.JsonFormat;
import jdk.nashorn.internal.objects.NativeRangeError;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jnlp.FileSaveService;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
//20220406分支测试2
@Controller
@RequestMapping("attendance")
public class AttendanceController {

    @Autowired
    private IAttendanceService attendanceService;

    @RequestMapping("list")
    @ResponseBody
    public List<Attendance> list() {
        return attendanceService.selectAll();
    }

    @RequestMapping("view")
    @RequiresPermissions(value = {"attendance:view","考勤界面"},logical = Logical.OR)
    public String view() {
        return "attendance";
    }

    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"attendance:saveOrUpdate","考勤新增/更新"},logical = Logical.OR)
    @ResponseBody
    public Object saveOrUpdate(Attendance attendance) {
        JSONResult result = new JSONResult();
        try {
            attendanceService.saveOrUpdate(attendance);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult attendance(AttendanceQueryObject qo) {
        return attendanceService.query(qo);
    }

    @RequestMapping("changeState")
    @ResponseBody
    public Object changeState(Long id) {
        JSONResult result = new JSONResult();
        try {
            attendanceService.changeState(id);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    //签到
    @RequestMapping("signIn")
    @ResponseBody
    public JSONResult signIn() throws ParseException {
        JSONResult result = new JSONResult();
        Date date = new Date();
        SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd");
        String s = s1.format(date);
        Attendance attendance = attendanceService.selectBySignInDate(s);

        if (attendance == null) {
            Employee emp = UserContext.getUserContext();
            Attendance att = new Attendance();
            att.setEmployee(emp);
            String inTime = "09:00:00";
            SimpleDateFormat s2 = new SimpleDateFormat("HH:mm:ss");
            String s3 = s2.format(date);
            Date date1 = s2.parse(s3);//实际签到时间
            Date date2 = s2.parse(inTime);//规定签到时间
            if (date1.getTime()>date2.getTime()) {
                att.setSignInState(false);
                attendanceService.saveOrUpdate(att);
                return result;
            } else {
                att.setSignInState(true);
                attendanceService.saveOrUpdate(att);
                return result;
            }
        }
        return result.remark("已签到,无需再签");
    }

    //签退
    @RequestMapping("signOut")
    @ResponseBody
    public Object signOut(Long id) throws ParseException {
        JSONResult result = new JSONResult();
        Date date = new Date();
        Attendance att = new Attendance();
        att.setId(id);
        String outTime = "18:00:00";
        SimpleDateFormat s2 = new SimpleDateFormat("HH:mm:ss");
        String s = s2.format(date);
        Date date1 = s2.parse(s);//实际签退时间
        Date date2 = s2.parse(outTime);//规定签退时间
        if (date1.getTime()<date2.getTime()) {
            att.setSignOutState(false);
            attendanceService.saveOrUpdate(att);
            return result;
        } else {
            att.setSignOutState(true);
            attendanceService.saveOrUpdate(att);
            return result;
        }
    }

    //补签
    @RequestMapping("supplement")
    @ResponseBody
    public JSONResult supplement(Date signInDate, Long id) throws ParseException {
            Boolean signInState = attendanceService.selectSignInStateByDate(signInDate);
            if (signInState != null) {//状态为true,说明已签到
                return new JSONResult().remark("已签到,无需补签");
            }
            Attendance attendance = new Attendance();
            attendance.setId(id);
            attendance.setPatchTime(new Date());
            if(attendance.getPatchTime() != null){
                return new JSONResult().remark("已补签,无需补签");
            }
            attendanceService.updatePatchTime(attendance);
            return new JSONResult();
    }

    @RequestMapping("judge")
    @ResponseBody
    public Object judge(Date newDate,Date oldDate){
        return null;
    }
}


